<template>
<div class="page-container">
    <div class="toolbar">
        <el-row>
            <el-tabs type="border-card">
                <el-tab-pane label="基本信息">
                    <el-form :inline="true" :model="dataForm" label-width="110px" style="text-align:center;" ref="dataForm" :rules="dataFormRules" :disabled="this.$route.query.disabled" class="form">
                        <el-row style="margin-top: -20px;">
                            <el-divider content-position="center">
                                <span style="font-size:16px;font-weight:800">基本信息</span>
                            </el-divider>
                            <el-row class="search-input">
                                <el-form-item label="" v-show="false">
                                    <el-input v-model="dataForm.id" placeholder="请输入"></el-input>
                                </el-form-item>
                                <el-form-item label="实体仓选择：" prop="storeCode">
                                    <el-select v-model="dataForm.storeCode" placeholder="请选择实体仓" @change="checkProduct" :disabled="!this.$route.query.id==0">
                                        <el-option v-for="item in stores" :key="item.id" :label="item.name" :value="item.code" :click="dataForm.deptId=item.id"></el-option>
                                    </el-select>
                                </el-form-item>
                                <el-form-item label="产品选择：" prop="productCode">
                                    <el-select v-model="dataForm.productCode" placeholder="请选择产品" :disabled="!this.$route.query.id==0">
                                        <el-option v-for="item in products" :key="item.productCode" :label="item.productName" :value="item.productCode"></el-option>
                                    </el-select>
                                </el-form-item>
                                <el-form-item label="产品预警值：" prop="warnValue">
                                    <el-input v-model.number="dataForm.warnValue" placeholder="请输入"></el-input>
                                </el-form-item>
                            </el-row>
                        </el-row>
                    </el-form>
                    <div class="tool-wrap">
                        <div class="left-area">
                            <el-button-group>
                                <el-button type="primary" size="small" @click="submitForm()" v-show="!this.$route.query.disabled">保存</el-button>
                                <el-button type="primary" size="small" @click="$router.go(-1)">取消</el-button>
                            </el-button-group>
                        </div>
                    </div>
                </el-tab-pane>
            </el-tabs>
        </el-row>
    </div>
</div>
</template>

<script>
//一.导包区
import jicTable from "@/components/jicComponentGallery/jicTable";
import {
    mapState
} from "vuex";
import ThemePicker from "@/components/ThemePicker";
import LangSelector from "@/components/LangSelector";
import VDistpicker from "v-distpicker";
import elTree from "@/views/tree/src/tree.vue";

//二.数据输出区
export default {
    //2.1 注册组件
    components: {
        jicTable,
        ThemePicker,
        LangSelector,
        VDistpicker,
        elTree
    },

    //2.2 数据初始化区
    data() {
        var checkAge = (rule, value, callback) => {
            if (!value) {
                return callback(new Error("预警值不能为空"));
            }
            setTimeout(() => {
                if (!Number.isInteger(value)) {
                    callback(new Error("请输入数字值"));
                } else {
                    if (value < 1) {
                        callback(new Error("预警值必须大于1"));
                    } else {
                        callback();
                    }
                }
            }, 1000);
        };
        return {
            stores: [], //实体仓列表数据
            products: [], //商品数据

            //2.2.1 公共数据初始化
            searchSize: "mini",

            // 2.2.2 form 表单数据初始化
            dataForm: {
                id: null,
                deptId: null,
                storeCode: null,
                productCode: null,
                warnValue: null,
                createTime: null,
                updateTime: null,
                creator: null,
                updater: null,
                deleteFlag: null
            },
            //trigger 为blur 表示丢失焦点;为change 表示值发生变化;
            dataFormRules: {
                storeCode: [{
                    required: true,
                    message: "请选择实体仓",
                    trigger: "change"
                }],
                productCode: [{
                    required: true,
                    message: "请选择产品",
                    trigger: "change"
                }],
                warnValue: [{
                    validator: checkAge,
                    trigger: "blur"
                }]
            }
        }; //return 完结;
    }, //data()方法 完结;

    //2.3 Vue 方法区
    methods: {
        //2.3.1 公共方法区

        //2.请求列表数据
        getDetail() {
            let this_ = this;
            if (
                this.$route.query != null &&
                this.$route.query.id != null &&
                this.$route.query.id != "" &&
                this.$route.query.id != "null" &&
                this.$route.query.id != "undefined"
            ) {
                let params = {};
                params.id = this.$route.query.id;
                this.utils.request.getMStoreStockWarningDetail(params, function (res) {
                    if (res.code === "0000") {
                        this_.dataForm = res.data;
                        this_.checkProduct(res.data.storeCode);
                    } else {
                        this_.$message({
                            message: "操作失败, " + data.msg,
                            type: "error"
                        });
                    }
                });
            }
        },

        submitForm: function () {
            this.$refs["dataForm"].validate(valid => {
                if (!valid) {
                    return;
                }
            });

            let this_ = this;
            this_.$refs.dataForm.validate(valid => {
                if (valid) {
                    this_.$confirm("确认提交吗？", "提示", {}).then(() => {
                        this_.editLoading = true;
                        let params = this.dataForm;
                        this_.utils.request.saveMStoreStockWarning(params, function (res) {
                            if (res.code === "0000") {
                                this_.$message({
                                    message: "操作成功",
                                    type: "success"
                                });
                            } else {
                                this_.$message({
                                    message: res.msg,
                                    type: "error"
                                });
                            }
                        });
                        this_.$router.go(-1);
                    });
                }
            });
        },
        // 7.页面弹窗处理方法
        add() {
            this.dialogVisible = true;
        },

        // 8.关闭弹窗
        handleClose() {
            this.dialogVisible = false;
        },

        back() {
            this.$router.go(-1);
        },
        //加载实体仓列表
        getStores() {
            let this_ = this;
            let params = this.dataForm;
            this_.utils.request.getMStoreList(params, function (res) {
                if (res.code === "0000") {
                    this_.stores = res.data;
                } else {
                    this_.$message({
                        message: "加载实体仓列表出错",
                        type: "error"
                    });
                }
            });
        },
        //根据店铺code查询店铺下所有商品列表
        checkProduct(code) {
            let this_ = this;
            this_.products = [];
            let params = this.dataForm;
            this_.utils.request.getProductListByStoreCode(params, function (res) {
                if (res.code === "0000") {
                    this_.products = res.data;
                } else {
                    this_.$message({
                        message: "加载实体仓列表出错",
                        type: "error"
                    });
                }
            });
        },
        checkAge() {}
        //==========2.3.1 JavaScript 自定义方法区==========
    },
    //1.初始化页面调用方法(dom元素绑定后再执行初始化数据操作)
    mounted() {
        //查询实体仓
        this.getStores();
        //查询产品
        this.getDetail();
    }
};
</script>

<style lang="scss" scoped>
/* ele组件样式重置 */
.common-content {
    padding: 10px;
    box-sizing: border-box;

    .search-wrap /deep/ {
        .el-form-item__label {
            height: 32px;
            line-height: 32px;
        }

        .el-form-item__content {
            height: 32px;
            line-height: 32px;
            width: 150px;
        }

        .el-input--mini .el-input__inner {
            height: 32px;
            line-height: 32px;
        }

        .el-date-editor.el-input,
        .el-date-editor.el-input__inner {
            width: 150px;
        }
    }

    /* 多日期样式重置 */
    .el-dater /deep/ .el-form-item__content {
        width: 330px;
    }

    /* 通用工具部分样式 */
    .tool-wrap {
        width: 100%;
        height: auto;
        position: relative;
        overflow: hidden;

        .left-area {
            float: left;
            width: 50%;
            text-align: left;

            button {
                margin-right: 15px;
                border-radius: 5px;
            }
        }

        .right-area {
            float: right;
            width: 50%;
            text-align: right;
        }
    }

    /* 表格部分样式 */
    .table-wrap {
        width: 100%;
        max-height: 600px;
        margin-top: 20px;
    }

    /* 弹窗样式 */
    .dialog-wrap /deep/ .el-dialog__footer {
        text-align: center !important;
    }
}
</style>
